package net.oschina.app.viewpagerfragment.MessageFragments;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.MApp;
import net.oschina.app.R;
import net.oschina.app.bean.Friend;
import net.oschina.app.bean.FriendsList;
import net.oschina.app.ui.adapter.MessageFenSAdapter;
import net.oschina.app.util.Md5Utils;
import net.oschina.app.util.MyHttpUtils;
import net.oschina.app.util.UIUtils;
import net.oschina.app.util.XmlUtils;
import net.oschina.app.viewpagerfragment.MyFragment.BaseItemFragment;

import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 * ?
 */

public class FenSiFragment extends BaseItemFragment {
    private XRecyclerView mXRecyclerView;

    @Override
    public void showItemFragmentView() {
        View view = View.inflate(UIUtils.getContext(), R.layout.item_fragment, null);
        mXRecyclerView = (XRecyclerView) view.findViewById(R.id.base_detail_xrv);
        LinearLayoutManager manager = new LinearLayoutManager(MApp.mContext);
        mXRecyclerView.setLayoutManager(manager);
        changeView(view);
    }

    @Override
    public void loadDates() {
        String url = "http://10.0.2.2:8080/oschina/list/friends_list0/page0.xml";
        String key = Md5Utils.md5(url);
        String result = MApp.mCacheUtils.getCache(key);
        if (result == null) {
            MyHttpUtils load = new MyHttpUtils();

            load.getDate(url, FriendsList.class, new MyHttpUtils.OngetDatesResult() {
                private List<Friend> mList;
                @Override
                public void Onsucessful(Object bean) {
                    FriendsList friendsList = (FriendsList) bean;
                    mList = friendsList.getList();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MessageFenSAdapter adapter = new MessageFenSAdapter(UIUtils.getContext(),mList);
                            mXRecyclerView.setAdapter(adapter);
                        }
                    });
                }

                @Override
                public void Onfail() {

                }
            });
        } else {
            FriendsList list = XmlUtils.toBean(FriendsList.class, result.getBytes());
            List<Friend> list1 = list.getList();
            MessageFenSAdapter adapter = new MessageFenSAdapter(UIUtils.getContext(),list1);
            mXRecyclerView.setAdapter(adapter);
        }


    }
}
